package com.swf.seed.netty.client.handler;

import java.util.Date;

import static org.jboss.netty.buffer.ChannelBuffers.*;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;


public class TimeClientHandler2 extends SimpleChannelHandler {
	  private final ChannelBuffer buf = dynamicBuffer();  
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		  ChannelBuffer m = (ChannelBuffer) e.getMessage();  
	        buf.writeBytes(m);  
	         System.out.println("in--"+System.currentTimeMillis());
	        if (buf.readableBytes() >= 4) {  
	            long currentTimeMillis = buf.readInt() * 1000L;  
	            System.out.println(new Date(currentTimeMillis));  
	            e.getChannel().close();  
	        }  
	}
	
	@Override  
	 
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {   

        e.getCause().printStackTrace();   

        e.getChannel().close();   

    }   
}
